using System;
using System.Runtime.InteropServices;

namespace OrbitOne.CRM
{
    /// <summary>
    /// Native methods wrapping the IDCRL unmanaged DLL.
    /// </summary>
    static class NativeMethods
    {
        [DllImport("msidcrl40.dll", EntryPoint = "InitializeEx", CharSet = CharSet.Unicode)]
        internal static extern int InitializeEx([In] ref System.Guid guid, [In] int lPPCRLVersion, [In] uint dwFlags, [In, MarshalAs(UnmanagedType.LPArray)] IDCRL_OPTION[] pOptions, [In] uint dwOptions);

        [DllImport("msidcrl40.dll", EntryPoint = "CreateIdentityHandle", CharSet = CharSet.Unicode)]
        internal static extern int CreateIdentityHandle([In, Optional, MarshalAs(UnmanagedType.LPWStr)] string wszMemberName, [In] uint dwFlags, out IntPtr pihIdentity);

        [DllImport("msidcrl40.dll", EntryPoint = "SetCredential", CharSet = CharSet.Unicode)]
        internal static extern int SetCredential([In] IntPtr hIdentity, [In, MarshalAs(UnmanagedType.LPWStr)] string wszCredType, [In, MarshalAs(UnmanagedType.LPWStr)] string wszCredValue);

        [DllImport("msidcrl40.dll", EntryPoint = "LogonIdentity", CharSet = CharSet.Unicode)]
        internal static extern int LogonIdentity([In] IntPtr hIdentity, [In, MarshalAs(UnmanagedType.LPWStr)] string wszPolicy, uint dwAuthFlags);

        [DllImport("msidcrl40.dll", EntryPoint = "GetAuthState", CharSet = CharSet.Unicode)]
        internal static extern int GetAuthState([In] IntPtr hIdentity, out int phrAuthState, out int phrAuthRequired, out int phrRequestStatus, out IntPtr szWebFlowUrl);

        [DllImport("msidcrl40.dll", EntryPoint = "AuthIdentityToService", CharSet = CharSet.Unicode)]
        internal static extern int AuthIdentityToService([In] IntPtr hIdentity, [In, MarshalAs(UnmanagedType.LPWStr)] string szServiceTarget, [In, Optional, MarshalAs(UnmanagedType.LPWStr)] string szServicePolicy, [In] uint dwTokenRequestFlags, out IntPtr szToken, out uint pdwResultFlags, out IntPtr ppbSessionKey, out uint pcbSessionKeyLength);

        [DllImport("msidcrl40.dll", EntryPoint = "CloseIdentityHandle", CharSet = CharSet.Unicode)]
        internal static extern int CloseIdentityHandle([In] IntPtr hIdentity);

        [DllImport("msidcrl40.dll", EntryPoint = "Uninitialize", CharSet = CharSet.Unicode)]
        internal static extern int Uninitialize();

        [DllImport("msidcrl40.dll", EntryPoint = "PassportFreeMemory", CharSet = CharSet.Unicode)]
        internal static extern int PassportFreeMemory([In, Out] IntPtr pMemoryToFree);
    }
}